package net.adetech.police.controller;


import com.alibaba.fastjson.JSONObject;
import net.adetech.cjg.innerservice.entity.Sqjl;
import net.adetech.cjg.innerservice.mapper.SqjlDao;
import net.adetech.police.api.IPoliceInterfaceService;
import net.adetech.police.entity.People;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
public class DemoController {

    @Autowired
    IPoliceInterfaceService policeInterfaceService;

    @RequestMapping(value = "/demo/sfz")
    public String sfz(String sfz) {
        People people = policeInterfaceService.getPeopleBySfzh(sfz);

        String zp = people.getXp();
        zp = transImage(zp).replace("\n","");
        System.out.println(zp);

        return "<img src=\"data:image/png;base64,"+people.getXp()+"\"/>";
    }


    @RequestMapping(value = "/sim/sfz",produces = "application/json")
    public String simSfz(String sfzmhm) {
        People people = policeInterfaceService.getPeopleBySfzh(sfzmhm);

//        String zp = people.getXp();
//        zp = transImage(zp).replace("\n","");

        return JSONObject.toJSONString(people);
    }


    @Autowired
    SqjlDao sqjlDao;

    @RequestMapping(value = "/sim/addCard",produces = "application/json")
    public String addCard(String sfzmhm,String type,String yxqx,String qfjg,String lsh) {

        People people = policeInterfaceService.getPeopleBySfzh(sfzmhm);
        String xm = people.getXm();
        String year = people.getCsrq().substring(0,4);
        String month = people.getCsrq().substring(4,6);
        String day = people.getCsrq().substring(6,8);
        String csrq = year+"-"+month+"-"+day;
        String address = people.getZzxz();
        String zp = people.getXp();
        String xb = people.getXb().equals("1")?"男":"女";
        String mz = "汉";
        zp = transImage(zp);


        JSONObject cardJson = new JSONObject();
        cardJson.put("xm",xm);
        cardJson.put("year",year);
        cardJson.put("month",Integer.parseInt(month));
        cardJson.put("day",Integer.parseInt(day));
        cardJson.put("csrq",csrq);
        cardJson.put("address",address);
        cardJson.put("zp",zp);
        cardJson.put("xb",xb);
        cardJson.put("xm",xm);
        cardJson.put("mz",mz);
        cardJson.put("zjyxq",yxqx);
        cardJson.put("qfjg",qfjg);
        cardJson.put("sfzmhm",sfzmhm);
        Sqjl sqjl = sqjlDao.getSqjlByLsh(lsh);
        JSONObject jsonObject = JSONObject.parseObject(sqjl.getYmJson());
        jsonObject.getJSONObject("form").put(type,cardJson);
        sqjlDao.updateYmJson(lsh,jsonObject.toJSONString());
        return jsonObject.toJSONString();


    }


    private int[] w = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

    private String[] suffix = new String[]{"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};

    private int getMod(String input) {
        int sum = 0;
        for (int i = 0; i < input.length(); i++) {
            String c = input.charAt(i) + "";
            int num = Integer.parseInt(c);
            sum += num * w[i];
        }
        return sum % 11;
    }

    private String getSuffixByMod(int mod)
    {
        return  suffix[mod];
    }

    @RequestMapping(value = "/cjg/querySfz")
    public String querySfz(String prefix) {

        List<People> resultList = new ArrayList<>();
        for (int i = 0; i < 1000; i+=2) {
            String index = "";
            if(i<10)
            {
                index = "00"+i;
            }
            else if(i<100)
            {
                index = "0"+i;
            }
            else
            {
                index = ""+i;
            }
            String suffix = getSuffixByMod(getMod(prefix+index));
            String sfzh = prefix +index+suffix;
            People people = policeInterfaceService.getPeopleBySfzh(sfzh);
            if(people!=null)
            {

                resultList.add(people);
                if(people.getXm().indexOf("王")>-1)
                {
                    System.out.println(people.getXm()+":"+sfzh);
                }
                System.out.println(people.getXm()+":"+sfzh);
            }
            System.err.println(index);

        }


        for (People p:resultList
             ) {
            System.out.println(p.getXm()+":"+p.getSfzh());

        }

        return "ok";
    }

    public String transImage(String zp) {
        String result = "";
        try {
            byte[] bytes1 = new BASE64Decoder().decodeBuffer(zp);
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
            BufferedImage bi = ImageIO.read(bais);
            Image image = (Image) bi;
            ImageIcon imageIcon = new ImageIcon(image);
            BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(),
                    BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
            g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
            int alpha = 0;
            for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
                for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
                    int rgb = bufferedImage.getRGB(j2, j1);

                    int R = (rgb & 0xff0000) >> 16;
                    int G = (rgb & 0xff00) >> 8;
                    int B = (rgb & 0xff);
                    if (((255 - R) < 12) && ((255 - G) < 12) && ((255 - B) < 12)) {
                        rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
                    }

                    bufferedImage.setRGB(j2, j1, rgb);

                }
            }

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "png", baos);
            byte[] bytes = baos.toByteArray();
            result = new BASE64Encoder().encode(bytes);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }





}
